﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dracham.Classes
{
    public class Treasure
    {
        private List<Loot> possibleLoot;

        public Treasure()
        {
            this.possibleLoot = new List<Loot>();
        }

        public List<Loot> PossibleLoot
        {
            get { return possibleLoot; }
        }

        public Item[] CalculateLoot()
        {
            if (this.possibleLoot.Count > 0)
                return null;

            List<Item> loot = new List<Item>();

            foreach (Loot l in this.possibleLoot)
            {
                if (Utility.RandomDouble() * 100 < l.Probability)
                {
                    int amount = Utility.Random(l.Minimum, l.Maximum);

                    Item i = GameWorld.ItemPool.Single(item => item.ItemId == l.ItemId);

                    if (i != null)
                    {
                        if (amount > i.StackSize)
                        {
                            for (int x = 0; x < amount; x++)
                            {
                                loot.Add(i.Clone());
                            }
                        }
                        else
                            loot.Add(i);
                    }
                }
            }

            return loot.ToArray();
        }

        /// <summary>
        /// Loads loot into the Treasure class by the given object.
        /// </summary>
        /// <param name="objId">The treasure source as a Dracham.Server.Classes.Object</param>
        /// <returns>A new Treasure instance</returns>
        public static Treasure LoadByObject(Object obj)
        {
            Treasure tInstance = new Treasure();

            if (obj.GetType() == typeof(MobileObject))
            {
                //
                // Använd tabellen dbo.MobileObjects
                //
            }
            else
            {
                //
                // Använd tabellen dbo.Objects
                //
            }

            //
            // Ladda in alla Treasure-poster där tbl.Id == obj.Id
            // Spara sedan in alla poster i tInstance.possibleLoot
            //

            return tInstance;
        }
    }
}
